package cn.whit.blog.service.impl;

import java.io.File;
import java.sql.SQLException;
import java.util.List;

import cn.whit.blog.dao.StatisticalDao;
import cn.whit.blog.dao.TblogDao;
import cn.whit.blog.dao.TcommitDao;
import cn.whit.blog.domain.Statistical;
import cn.whit.blog.domain.StatisticalType;
import cn.whit.blog.domain.Tblog;
import cn.whit.blog.domain.Tcommit;
import cn.whit.blog.service.TblogService;
import cn.whit.blog.util.BeanFactory;
import cn.whit.blog.util.DynamicQuery;
import cn.whit.blog.util.PageModel;
import cn.whit.blog.util.PropertyUtil;
import cn.whit.blog.util.UploadUtils;

public class TblogServiceImpl implements TblogService{
	
	TblogDao tblogDao = (TblogDao)BeanFactory.createObject("tblogDao");
	
	StatisticalDao statisticalDao = (StatisticalDao)BeanFactory.createObject("statisticalDao");
	
	TcommitDao tTcommitDao = (TcommitDao)BeanFactory.createObject("tcommitDao");
	
	@Override
	public PageModel findByPageAndQuery(int startIndex, int pageSize, DynamicQuery query) throws Exception {
		//startIndex 从第一页，索引 1 开始
		int totalRecords = tblogDao.findTotalRecordsAndQuery(query);
		PageModel pm = new PageModel(startIndex,totalRecords,pageSize);
		List<Tblog> list = tblogDao.findAllWithPageAndQuery(pm.getStartIndex(),pm.getPageSize(),query);
		pm.setList(list);
		//3_关联url
		pm.setUrl("TblogServlet?method=tblogList");
		return pm;
	}
	
	@Override
	public List<Tblog> findAllWithPageAndQuery(int startIndex, int pageSize, DynamicQuery query) throws Exception {
		return tblogDao.findAllWithPageAndQuery(startIndex, pageSize, query);
	}

	@Override
	public int findTotalRecordsAndQuery(DynamicQuery query) throws Exception {
		return tblogDao.findTotalRecordsAndQuery(query);
	}

	@Override
	public long save(Tblog tblog) throws SQLException {
		return tblogDao.save( tblog);
	}

	@Override
	public Tblog findById(String id) throws SQLException {
		return tblogDao.findById(id);
	}

	public static void main(String[] args) throws Exception {
		
		TblogServiceImpl tc = new TblogServiceImpl();
		
		int startIndex = 0;
		int pageSize = 3;
		DynamicQuery query = new DynamicQuery();;
		List<Tblog> ones = tc.findAllWithPageAndQuery(startIndex, pageSize, query);
		System.out.println(ones.size());
		for(Tblog one : ones) {
			System.out.println(one);
		}
		
	}

	@Override
	public void addvote(String id, String userid,String username,StatisticalType statisticalType) throws SQLException {
		//如果该用户id对该博客没有点赞过，则点赞数+1
		List<Statistical> ones = statisticalDao.findAllByUserIdAndTblogIdAndType(userid, id, statisticalType);
		if(ones != null && ones.size() > 0) {
			System.out.println("记录过");
		}else {
			//博客的点赞数+1
			tblogDao.updateNum(id,statisticalType);
			//统计表数据更新
			statisticalDao.add(userid,username, id, statisticalType);
		}
		
		
	}

	@Override
	public void deleteById(String s) throws SQLException {
		// 删除博客的时候，需要先把该博客的评论数据，点赞数据等，先删除
		tTcommitDao.deleteByVoteId(s);
		statisticalDao.deleteByVoteId(s);
		//把博客关联的图片文件删除 Todo
		Tblog one = tblogDao.findById(s);
		String picurls = one.getPicurls();
		
		String realPath = "";
		// realPath 在服务商上，需要取得绝对路径
		realPath = PropertyUtil.getProperty("uploadPath");
		 
		// 在服务端创建一个空文件(后缀必须和上传到服务端的文件名后缀一致)
		File removeFile = new File(realPath+picurls);
		if (removeFile.exists()) {
			removeFile.delete();
			
		}
		
		tblogDao.deleteById(s);
		
	}

	@Override
	public List<Tblog> findByUserId(String s) throws SQLException {
		return tblogDao.findByUserId(s);
	}
}
